package com.example.agriapp.view.fragment;

import static android.app.Activity.RESULT_OK;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.agriapp.R;
import com.example.agriapp.model.FeedContract;
import com.example.agriapp.model.bean.FeedItem;
import com.example.agriapp.model.bean.User;
import com.example.agriapp.presenter.FeedPresenter;
import com.example.agriapp.view.activity.LoginActivity;
import com.example.agriapp.view.activity.MainActivity;
import com.example.agriapp.view.activity.MeActivity;
import com.example.agriapp.view.activity.PostActivity;
import com.example.agriapp.adapter.FeedAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class FriendsCircleFragment extends Fragment implements FeedContract.View, FeedAdapter.OnFeedInteractionListener {
    private static final int REQUEST_POST = 10;
    private static final String TAG = "FriendsCircleFragment";
    private View view;
    private TextView tvTuiJian,tvMe;
    private TextView tvGuanZhu;

    // private PostAdapter mAdapter;
    public FriendsCircleFragment() {
        // Required empty public constructor
    }
    public static FriendsCircleFragment newInstance() {
        return new FriendsCircleFragment();
    }

    private RecyclerView recyclerView;
    private FeedAdapter adapter;
    private FeedContract.Presenter presenter;

    private List<FeedItem> mFeedItems = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_friends_circle, container, false);
        // 初始化Presenter
        presenter = new FeedPresenter(this, getContext().getApplicationContext());

        // 初始化视图
        recyclerView = view.findViewById(R.id.recyclerView);
        tvTuiJian = view.findViewById(R.id.tvTuiJian);
        tvGuanZhu = view.findViewById(R.id.tvGuanZhu);
        tvMe = view.findViewById(R.id.tvMe);
        setTextSize(0);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        // 设置发布按钮
        view.findViewById(R.id.btnPost).setOnClickListener(v -> {
            // 打开发布界面
              startActivityForResult(new Intent(getContext(), PostActivity.class), REQUEST_POST);
        });

        tvGuanZhu.setOnClickListener(v -> {
            setTextSize(1);

        });

        tvTuiJian.setOnClickListener(v -> {
            setTextSize(0);
        });
        User user = (User) getActivity().getIntent().getSerializableExtra("USER");
        tvMe.setOnClickListener(v->{
            Intent intent = new Intent(getActivity(), MeActivity.class);
            intent.putExtra("USER",user );
            startActivity(intent);
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        setTextSize(0);
    }

    @Override
    public void onLikeClick(FeedItem item) {
        if (item.getLikes()==0) {
            presenter.likeFeed(item);
        }else {
            presenter.unlikeFeed(item);
        }
    }

    @Override
    public void onCommentClick(FeedItem item) {

    }

    @Override
    public void onShareClick(FeedItem item) {

    }

    @Override
    public void onFollowClick(FeedItem item) {
        if (item.isFollowed()) {
            presenter.unfollowUser(item);
        } else {
            presenter.followUser(item);
        }
    }

    @Override
    public void onShopClick(FeedItem item) {

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_POST && resultCode == RESULT_OK) {
            String content = data.getStringExtra("content");
            Uri imageUri = data.getParcelableExtra("imageUri");
            presenter.postNewFeed(content, imageUri);
        }
    }

    @SuppressLint("ResourceAsColor")
    public void setTextSize(int type){

        tvTuiJian.setTextColor(type==0?Color.BLACK:R.color.colorNavUnselected);
        tvTuiJian.setTextSize(type==0?20:16);
        tvTuiJian.setTypeface(null, type==0?Typeface.BOLD:Typeface.NORMAL);

        tvGuanZhu.setTextColor(type==1?Color.BLACK:R.color.colorNavUnselected);
        tvGuanZhu.setTextSize(type==1?20:16);
        tvGuanZhu.setTypeface(null, type==1?Typeface.BOLD:Typeface.NORMAL);
        presenter.loadFeeds(type);
    }
    @Override
    public void showFeeds(List<FeedItem> feeds) {
        Log.e(TAG, "showFeeds: "+feeds.size() );
        adapter = new FeedAdapter(getContext(),feeds, this);
        recyclerView.setAdapter(adapter);
       // adapter.updateData(feeds);
    }

    @Override
    public void updateFeedItem(FeedItem item) {
        adapter.updateItem(item);
    }

    @Override
    public void showLoading() {
        
    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError(String message) {

    }

    @Override
    public void navigateToShop(String shopName) {
        Toast.makeText(getContext(), "进入" + shopName + "的店铺", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showPostSuccess() {
        Toast.makeText(getContext(), "发布成功", Toast.LENGTH_SHORT).show();
        setTextSize(0);
    }



    // 实现其他接口方法...

}